package com.it.btyw.modules.user.activity

import LoginVo
import android.util.Log
import android.widget.Toast
import androidx.activity.viewModels
import com.it.btyw.base.BaseActivity
import com.it.btyw.databinding.ActivityLoginBinding
import com.it.btyw.dialog.ProgressDialog
import com.it.btyw.modules.main.viewmodel.MainViewModel
import com.it.btyw.modules.station.viewmodel.GQStationViewModel
import com.it.btyw.modules.user.viewmodel.LoginViewModel
import com.it.data.manager.AppCacheManager
import com.tencent.mmkv.MMKV

class LoginActivity : BaseActivity<ActivityLoginBinding>() {
    companion object {
        const val TAG = "LoginActivity"
    }

    private val loginViewModel by viewModels<LoginViewModel>()
    private val gqStationViewModel by viewModels<GQStationViewModel>()
    private val mainViewModel by viewModels<MainViewModel>()
    override fun initObserver() {
        loginViewModel.loginLiveData.observe(this) {
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_SHORT).show()
            }
        }
        gqStationViewModel.pageGQStationVoListLiveData.observe(this) {
            Log.d(TAG, "initObserver: pageGQStationVoListLiveData ${it.data?.list}")
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "查询测站列表成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_SHORT).show()
            }
        }
        mainViewModel.menuVoListLiveData.observe(this) {
            Log.d(TAG, "initObserver: menuVoListLiveData ${it.data}")
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "查询菜单列表成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_SHORT).show()
            }
        }
        mainViewModel.nodeVoListLiveData.observe(this) {
            Log.d(TAG, "initObserver: nodeVoListLiveData ${it.data}")
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "查询节点列表成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_SHORT).show()
            }
        }
        mainViewModel.sttpVoListLiveData.observe(this) {
            Log.d(TAG, "initObserver: sttpVoListLiveData ${it.data}")
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "查询sttp列表成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_SHORT).show()
            }
        }
        mainViewModel.gqywxxVoLiveData.observe(this) {
            Log.d(TAG, "initObserver: gqywxxVoLiveData ${it.data}")
            ProgressDialog.hide()
        }
        gqStationViewModel.pageWtGQVoListLiveData.observe(this) {
            Log.d(TAG, "initObserver: pageWtGQVoListLiveData ${it.data?.list}")
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "查询运维记录列表成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_SHORT).show()
            }
        }
        mainViewModel.quaTypeVoListLiveData.observe(this) {
            Log.d(TAG, "initObserver: quaTypeVoListLiveData ${it.data}")
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun initListener() {
        binding.btnLogin.setOnClickListener {
            ProgressDialog.show(this)
            loginViewModel.login(loginName = "ll", password = "qwer@.123")
        }
        binding.btnGqStationList.setOnClickListener {
            ProgressDialog.show(this)
            val userId = MMKV.defaultMMKV()
                .decodeParcelable(AppCacheManager.KEY_LOGIN_DATA, LoginVo::class.java)?.id
            userId?.let { uId -> gqStationViewModel.getGQStationList(userId = uId, 1, 20) }
        }
        binding.btnQuery.setOnClickListener {
            ProgressDialog.show(this)
//            val userId = MMKV.defaultMMKV()
//                .decodeParcelable(AppCacheManager.KEY_LOGIN_DATA, LoginVo::class.java)?.id
//            val depId = MMKV.defaultMMKV()
//                .decodeParcelable(AppCacheManager.KEY_LOGIN_DATA, LoginVo::class.java)?.depId
//
//            userId?.let { uId -> mainViewModel.findMenuListByUserid(userId = uId) }
//            depId?.let { dId -> mainViewModel.getNodeByAd(depId = dId) }
//            mainViewModel.getSttp()
//            mainViewModel.getGQYWXX(STCD = "00001118")
//            gqStationViewModel.getWtGQ(pageNum = 1, pageSize = 10, stcd = "0000111", type = 2)
            mainViewModel.getQuaType()

        }

    }

    override fun initData() {

    }

    override fun initView() {

    }

    override fun setContentView() {
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}